package com.zero.xiaodai.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.base.Strings;
import com.zero.xiaodai.dao.HsIdentityDao;
import com.zero.xiaodai.dao.HsUserDao;
import com.zero.xiaodai.dao.HsUserIdentityDao;
import com.zero.xiaodai.pojo.HsIdentity;
import com.zero.xiaodai.pojo.HsUser;
import com.zero.xiaodai.pojo.HsUserIdentity;
import com.zero.xiaodai.service.ExcelService;
import com.zero.xiaodai.utils.ExcelUtilZ;
import com.zero.xiaodai.utils.ListUtil;



@Service
@Transactional(propagation = Propagation.REQUIRED)
public class ExcelServiceImpl implements ExcelService {

	@Resource
	private HsUserDao userDao; 
	@Resource
	private HsUserIdentityDao userIdentityDao; 
	@Resource
	private HsIdentityDao hsIdentityDao; 
	
	@Override
	public void importExcelData(MultipartFile file) {
		// TODO Auto-generated method stub
		String[] properties=new String[9];
		properties[0]="userName";
		properties[1]="userMobile";
		properties[2]="identityName";
		properties[3]="";
		properties[4]="lawOffice";
		properties[5]="";
		properties[6]="";
		properties[7]="";
		properties[8]="";
		List<HsUser> list=new ArrayList<>();
		List<HsIdentity> identityList=new ArrayList<>();
		try {
			list = ExcelUtilZ.analysisExcle(file, properties, HsUser.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<HsUserIdentity> uiList=new ArrayList<>();
		List<HsUser> newList=new ArrayList<>();
		List<String> identities=new ArrayList<>();
		Map<String, Object> map=new HashMap<>();
		for(HsUser user:list) {
			if(Strings.isNullOrEmpty(user.getUserMobile())) {
				continue;
			}
			String idName=user.getIdentityName();
			if(Strings.isNullOrEmpty(idName.trim())) {
				continue;
			}
			if(idName.contains("（其他）")) {
				idName=idName.replace("（其他）", "");
			}
			if(idName.contains("兼")) {
				idName=idName.replace("（", "");
				idName=idName.replace("兼", "");
				idName=idName.replace("）", "");
				idName=idName.replace("(", "");
				idName=idName.replace(")", "");
			}
					if(idName.contains("（杭州）")||user.getLawOffice().contains("杭州")) {
						user.setLawOffice("3");
						idName=idName.replace("（杭州）", "");
					}else  if(idName.contains("（慈溪）")||user.getLawOffice().contains("慈溪")) {
						user.setLawOffice("4");
						idName=idName.replace("（慈溪）", "");
					}else if(idName.contains("（上海）")||user.getLawOffice().contains("上海")) {
						user.setLawOffice("2");
						idName=idName.replace("（上海）", "");
					}else if(idName.contains("(上海）")) {
						user.setLawOffice("2");
						idName=idName.replace("(上海）", "");
					}else if(idName.contains("（北仑）")||user.getLawOffice().contains("北仑")) {
						user.setLawOffice("5");
						idName=idName.replace("（北仑）", "");
					}else {
						user.setLawOffice("1");
					}
					
			if(!identities.contains(idName)) {
				identities.add(idName);
				HsIdentity identity=new HsIdentity();
				String identityId=UUID.randomUUID().toString();
				identity.setId(identityId);
				identity.setIdentityName(idName);
				identityList.add(identity);
				map.put(idName, identityId);
			}
			HsUser u=null;
			if(!Strings.isNullOrEmpty(user.getUserMobile())) {
				List<HsUser> ulist=userDao.queryByMobile(user.getUserMobile(),null);
				if(!ListUtil.isNullOrEmpty(ulist)) {
					u=ulist.get(0);
				}
			}
			if(u != null) {
				String uidentityId=String.valueOf(map.get(idName));
				HsUserIdentity ui=new HsUserIdentity();
				ui.setId(UUID.randomUUID().toString());
				ui.setIdentityId(uidentityId);
				ui.setUserId(u.getId());
				uiList.add(ui);
				u.setLawOffice(user.getLawOffice());
				userDao.update(u);
				continue;
			}
			
			if(idName.contains("审批负责人")) {
				for(String name:user.getUserName().split("、")) {
					List<HsUser> ulist=userDao.queryByMobile(null,name);
					if(!ListUtil.isNullOrEmpty(ulist)) {
						u=ulist.get(0);
					}
					HsUserIdentity ui=new HsUserIdentity();
					ui.setId(UUID.randomUUID().toString());
					ui.setIdentityId(String.valueOf(map.get(idName)));
					ui.setUserId(u.getId());
					uiList.add(ui);
				}
			}else {
				user.setId(UUID.randomUUID().toString());
				user.setIsLock(0);
				user.setCanUpload(1);
				newList.add(user);
				
				HsUserIdentity ui=new HsUserIdentity();
				ui.setId(UUID.randomUUID().toString());
				ui.setIdentityId(String.valueOf(map.get(idName)));
				ui.setUserId(user.getId());
				uiList.add(ui);
				continue;
			}
			
			
			
			
			
			
			
		}
		
		if(!ListUtil.isNullOrEmpty(newList)) {
			userDao.adds(newList);
		}
		if(!ListUtil.isNullOrEmpty(uiList)) {
			userIdentityDao.adds(uiList);
		}
		if(!ListUtil.isNullOrEmpty(identityList)) {
			hsIdentityDao.adds(identityList);
		}
		
		
		//list=list.stream().filter(d->!Strings.isNullOrEmpty(d.getTradingContract())).collect(Collectors.toList());
		//return list;

	}

}
